package com.njxz.demo.controller;


import com.njxz.demo.domain.Book;
import com.njxz.demo.domain.BookCategory;
import com.njxz.demo.domain.Vo.BookVo;
import com.njxz.demo.domain.result.Result;
import com.njxz.demo.service.IAdminService;
import com.njxz.demo.service.IBookCategoryService;
import com.njxz.demo.service.IBookService;
import com.njxz.demo.utils.page.Page;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/bookmanage")
@CrossOrigin
public class AdminBookManageController {
    @Autowired
    private IBookCategoryService bookCategoryService;

    @Autowired
    private IAdminService adminService;

    @Autowired
    private IBookService bookService;



    /**
     * 查询所有图书类别
     * @param pageNum
     * @return
     */
    @GetMapping("/getBookCategory")
    public Result getBookCategoryByPageNum(@RequestParam("pageNum") int pageNum)
    {
        Page<BookCategory> page=bookCategoryService.selectBookCategoryByPageNum(pageNum);

        return Result.ok(page);
    }

    /**
     * 新建书籍种类
     * @param
     * @return
     */
    @PostMapping("/addBookCategory")
    @ResponseBody
    public Result addBookCategory(BookCategory bookCategory) //@Request是接收前端传来的数据
    {
        System.out.println(bookCategory);
        boolean b=adminService.addBookCategory(bookCategory);
        if(b){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 根据书籍种类id删除种类
     * @param bookCategoryId
     * @return
     */
    @PostMapping("/deleteCategory")
    @ResponseBody
    public Result deleteBookCategoryById(@RequestParam("bookCategoryId") int bookCategoryId){
        System.out.println(bookCategoryId);
        int res=bookCategoryService.deleteBookCategoryById(bookCategoryId);
        if(res>0){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 管理员录入新书
     * @param book
     * @return
     */
    @PostMapping("/addBook")
    @ResponseBody
    public Result addBook(@RequestBody Book book){
        boolean res=adminService.addBook(book);
        if(res){
            return Result.ok();
        }
        return Result.fail();
    }

    /**
     * 查询所有书籍
     * @param pageNum
     * @return
     */
    @GetMapping("/findAllBooks")
    public Result findAllBooks(@RequestParam("pageNum") int pageNum)
    {
        Page<BookVo> page=bookService.findAllBooks(pageNum);

        return Result.ok(page);
    }
    /**
     * 根据书籍id删除书籍
     */
    @PostMapping("/deleteBook")
    @ResponseBody
    public Result deleteBookById(@RequestParam("bookId") int bookId){
        System.out.println(bookId);
        int res=bookService.deleteBookById(bookId);
        if(res>0){
            return Result.ok();
        }
        return Result.fail();
    }



}
